# -*- coding: utf-8 -*-
import MySQLdb
import sys
import re
import random
from datetime import *  
import time 

user_id = re.split(',',sys.argv[1])
itemid = ['40001011','40001021','40002021','40003021','40004021','40011011','40011021','40012021']
datanum = sys.argv[2]
beginTime =  "2013-02-01 22:56:36"
endTime = "2013-02-06 22:56:36"
bt = datetime.fromtimestamp(time.mktime(time.strptime(beginTime,"%Y-%m-%d %H:%M:%S")))
et = datetime.fromtimestamp(time.mktime(time.strptime(endTime,"%Y-%m-%d %H:%M:%S")))
deltadays = (et - bt).days
print bt + timedelta(days =10)

print sys.argv[1],sys.argv[2]
print user_id

try:    
    conn=MySQLdb.connect(host='10.235.152.7',user='admin',passwd='admin',port=2828) 
    cur=conn.cursor() 
    memberCount=cur.execute('select max(id) from hj_user_bill;') 
    maxid = cur.fetchone(); 
    print maxid
    
    for index in range(int(datanum)):   
        id = int(maxid[0]) + index +1
        modid = int(id)%1000
        id = "%d" % id
        smodid = "%3d" % modid
        i = random.randint(0, len(user_id)-1)
        j = random.randint(0, len(itemid)-1)
        d = random.randint(0, deltadays)
        gmt_c = str( bt + timedelta(days =d))
        gmt_m = str( bt + timedelta(days =d))
        print "gmt_c:" + gmt_c
        sql = "insert into  hj_user_bill  (mod_id, id ,  user_id ,  user_nick ,  bill_cycle ,  bill_level ,  item_id ,  cur_order_amount ,  cur_total_amount ,  cur_unpay_amount ,  history_pay_amount ,  unpay_amount ,  file_status,  invoice_status ,  gmt_create ,  gmt_modified  ,  preceding_unpay_amount ,  red_amount ,  seller_type ,  history_unpay_amount ,  preceding_history_unpay_amount ,cur_month_order_amount) values('"+smodid+"','"+id+"','"+user_id[i]+"','test','201301','3','"+itemid[j]+"','10000','10000','0','500','0','0','1','"+gmt_c+"','"+gmt_m+"','0','501','2',NULL,NULL,'10000');"
        print sql
        cur=conn.cursor() 
        cur.execute(sql)
        
        conn.commit() 
  
    cur.close() 
    conn.close() 
    
except MySQLdb.Error,e: 
    print "Mysql Error %d: %s" % (e.args[0], e.args[1]) 
